﻿using Growatt.ShinebusService.Model;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using TestFile.Model;

public class FileDownloader
{
    private readonly IHttpClientFactory httpClientFactory;

    public FileDownloader(IHttpClientFactory httpClientFactory)
    {
        this.httpClientFactory = httpClientFactory;
    }
    public async Task<FirmwareUploadInfoResponse> DownloadFileAsync(string fileUrl, string destinationPath)
    {
        using (HttpClient client = httpClientFactory.CreateClient("OSS"))
        {
            try
            {
                // 发送HTTP GET请求以获取文件
                HttpResponseMessage response = await client.GetAsync(fileUrl);
                response.EnsureSuccessStatusCode(); // 确保请求成功

                // 将文件写入本地路径
                using (Stream fileStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    await response.Content.CopyToAsync(fileStream);
                }
                return new FirmwareUploadInfoResponse(string.Empty, 1, "文件已成功!");
            }
            catch (Exception ex)
            {
                return new FirmwareUploadInfoResponse(string.Empty, 0, "下载文件时出错!");
            }
        }
    }


}